package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.DishComment;
import com.example.mapper.DishCommentMapper;
import com.example.service.DishCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishCommentServiceImpl extends ServiceImpl<DishCommentMapper, DishComment> implements DishCommentService {
    
    @Override
    public List<DishComment> getCommentsByDishId(Long dishId) {
        LambdaQueryWrapper<DishComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishComment::getDishId, dishId)
            .eq(DishComment::getStatus, 1) // 只查询已通过审核的评价
            .orderByDesc(DishComment::getCreateTime);
        return this.list(wrapper);
    }
    
    @Override
    public List<DishComment> getCommentsByStudentId(Long studentId) {
        LambdaQueryWrapper<DishComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishComment::getUserId, studentId)
            .orderByDesc(DishComment::getCreateTime);
        return this.list(wrapper);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addComment(DishComment comment) {
        // 设置初始状态为待审核
        comment.setStatus(0);
        return this.save(comment);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateComment(DishComment comment) {
        return this.updateById(comment);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteComment(Long id) {
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean reviewComment(Long id, Integer status) {
        DishComment comment = this.getById(id);
        if (comment == null) {
            return false;
        }
        comment.setStatus(status);
        return this.updateById(comment);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean likeComment(Long id) {
        DishComment comment = this.getById(id);
        if (comment == null) {
            return false;
        }
        // 由于移除了likeCount字段，这个方法暂时不实现具体逻辑
        return true;
    }
    
    @Override
    public double getAverageRating(Long dishId) {
        LambdaQueryWrapper<DishComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishComment::getDishId, dishId)
            .eq(DishComment::getStatus, 1);
        List<DishComment> comments = this.list(wrapper);
        if (comments.isEmpty()) {
            return 0.0;
        }
        double sum = comments.stream().mapToInt(DishComment::getRating).sum();
        return sum / comments.size();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, Integer status) {
        DishComment comment = getById(id);
        if (comment == null) {
            return false;
        }
        comment.setStatus(status);
        return updateById(comment);
    }
} 